package com.example.big_event.controller;

import com.example.big_event.pojo.Category;
import com.example.big_event.pojo.Result;
import com.example.big_event.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    //添加文章
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return  Result.success();
    }

    //获取文章列表，展示所有文章
    @GetMapping
    public Result<List<Category>>list(){
        List<Category> cs=categoryService.list();
        return Result.success(cs);
    }

    //获取某一篇文章分类详情
    //根据文章的id返回文章详情
    @GetMapping("/detail")
    public Result<Category>detail(Integer id){
        Category c=categoryService.findById(id);
        return Result.success(c);
    }


    //更新文章的，比如更新文章的名字和别名
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success();
    }
}
